package br.com.ordersys.web.client.modules.main;

import br.com.ordersys.web.client.modules.broker.CallBack;
import br.com.ordersys.web.client.modules.broker.ServiceBroker;
import br.com.ordersys.web.client.modules.broker.ServiceCall;
import br.com.ordersys.web.client.modules.broker.ServiceResponse;
import br.com.ordersys.web.client.modules.uicomp.BasicPopup;
import br.com.ordersys.web.client.modules.uicomp.BasicResources;
import br.com.ordersys.web.client.modules.utils.CryptUtils;
import br.com.ordersys.web.client.modules.utils.I18nBundle;
import br.com.ordersys.web.client.modules.utils.UIUtils;
import br.com.ordersys.web.client.modules.utils.XMLUtils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;

public class PasswordChangeForm {
	private HTML lblMsg;
	private I18nBundle i18n = new I18nBundle("mainWindow");
	
	static{
		BasicResources.INSTANCE.basicStyle().ensureInjected();
	}

	public void show() {
		BasicPopup popup = new BasicPopup(i18n.get("title_password_change_form"), false, true);

		popup.setBodyPanel(buildUI(popup));
		popup.setSize("300px", "200px");

		popup.center();
	}

	private Panel buildUI(final BasicPopup popup) {
		VerticalPanel vpnl = new VerticalPanel();
		UIUtils.setWidgetStyle(vpnl, "padding: 20px 6px 0; width: 100%; height: 100%");

		VerticalPanel formPanel = new VerticalPanel();
		formPanel.setWidth("100%");

		final PasswordTextBox txtPass = new PasswordTextBox();
		txtPass.addStyleName(BasicResources.INSTANCE.basicStyle().defaultTextBox());
		final PasswordTextBox txtConfirm = new PasswordTextBox();
		txtConfirm.addStyleName(BasicResources.INSTANCE.basicStyle().defaultTextBox());

		formPanel.add(new Label(i18n.get("lbl_new_password")));
		formPanel.add(txtPass);
		txtPass.setWidth("100%");
		UIUtils.setParentStyle(txtPass, "paddingRight: 6px");

		formPanel.add(UIUtils.buildEmptySpace("1px", "10px"));
		formPanel.add(new Label(i18n.get("lbl_retype_password")));
		formPanel.add(txtConfirm);
		txtConfirm.setWidth("100%");
		UIUtils.setParentStyle(txtConfirm, "paddingRight: 6px");

		lblMsg = new HTML("&nbsp;");
		UIUtils.setWidgetStyle(lblMsg, "color: red; padding: 4px 0 0");
		formPanel.add(lblMsg);

		vpnl.add(formPanel);

		HorizontalPanel hpnl = new HorizontalPanel();
		hpnl.setSpacing(15);

		vpnl.add(hpnl);

		vpnl.setCellHorizontalAlignment(hpnl, VerticalPanel.ALIGN_CENTER);
		vpnl.setCellVerticalAlignment(hpnl, VerticalPanel.ALIGN_BOTTOM);

		Button btnCancel = new Button(i18n.get("btn_cancel"), new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				popup.hide();
			}
		});

		btnCancel.setStyleName(BasicResources.INSTANCE.basicStyle().defaultButton());
		hpnl.add(btnCancel);

		Button btnConfirm = new Button(i18n.get("btn_confirm"), new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				changePassword(txtPass.getText(), txtConfirm.getText(), popup);
			}

		});

		btnConfirm.setStyleName(BasicResources.INSTANCE.basicStyle().defaultButton());

		hpnl.add(btnConfirm);

		return vpnl;
	}

	private void changePassword(String pass, String confirmedPass, final BasicPopup popup) {
		if (pass == null) {
			pass = "";
		}

		if (confirmedPass == null) {
			confirmedPass = "";
		}

		if (!confirmedPass.equals(pass)) {
			lblMsg.setHTML(i18n.get("msg_error_entered_password_dont_match"));
			return;
		}
		
		lblMsg.setHTML("&nbsp;");

		ServiceCall sc = new ServiceCall("user", "changePassword");

		Document xml = XMLParser.createDocument();

		Element reqBody = xml.createElement("obj");

		reqBody.appendChild(XMLUtils.createElement(xml, "password", CryptUtils.getMD5for(pass)));

		sc.setServiceBody(reqBody);
		sc.setHandleErrors(true);

		sc.setResponseCallBack(new CallBack() {
			@Override
			public void onResponse(ServiceResponse response) {
				if (response.getStatus() == 0) {
					popup.hide();
				} else {
					lblMsg.setText(response.getMessage());
				}
			}
		});

		ServiceBroker sb = new ServiceBroker();

		sb.call(sc);

	}
}
